import static joc.Static.NO;

import java.util.ArrayList;

import com.saurik.uicaboodle.Main;

import joc.Message;
import obc.CGRect;
import obc.CGSize;
import obc.NSIndexPath;
import obc.NSMutableArray;
import obc.UIApplication;
import obc.UIHardware;
import obc.UINavigationBar;
import obc.UINavigationItem;
import obc.UITableView;
import obc.UITableViewCell;
import obc.UIView;
import obc.UIWindow;

public class HelloJava
    extends UIApplication
{

private UIWindow window;

private static class Contact {
    public String first;
    public String last;

    public Contact(String first, String last) {
        this.first = first;
        this.last = last;
    }

    public String getName() {
        String name = first;
        if (last != null)
            name += " " + last;
        return name;
    }
}

public static class Section {
    public ArrayList<Contact> rows = new ArrayList<Contact>();
    public String title;

    public Section(String title) {
        this.title = title;
    }
}

private ArrayList<Section> sections_;

@Message public final int numberOfSectionsInTableView$(UITableView table){
	return sections_.size();
}

@Message public final int tableView$numberOfRowsInSection$(UITableView table, int sectionIndex){
	return sections_.get(sectionIndex).rows.size();			
}

@Message public final UITableViewCell tableView$cellForRowAtIndexPath$(UITableView table, NSIndexPath indexPath){
    UITableViewCell cell = new UITableViewCell().init();
    cell.setText$(sections_.get(indexPath.section()).rows.get(indexPath.row()).getName());
    return cell;
}

@Message public final void tableView$didSelectRowAtIndexPath$(UITableView table, NSIndexPath indexPath){
	System.out.println(sections_.get(indexPath.section()).rows.get(indexPath.row()).getName() + " selected");
}

@Message public String tableView$titleForHeaderInSection$(UITableView table, int section) {
    return sections_.get(section).title;
}

@Message public NSMutableArray sectionIndexTitlesForTableView$(UITableView tableView) {		
	NSMutableArray titles = new NSMutableArray().initWithCapacity$(sections_.size());
	for(Section s : sections_){
		titles.addObject$(s.title);			
	}
	return titles;
}


@Message public int tableView$sectionForSectionIndexTitle$atIndex$(UITableView tableView, String title, int index) {
    return index;
}

@Message public void applicationDidFinishLaunching$(Object unused)
    throws Exception
{
    System.out.println("started");
    sections_ = new ArrayList<Section>();

    SQLite.Database ab = new SQLite.Database();
    ab.open(userHomeDirectory().toString() + "/Library/AddressBook/AddressBook.sqlitedb", 0666); try {
    	SQLite.Stmt st = ab.prepare("select first, last from ABPerson where first is not null order by first"); try {
			char letter = 0;
    		while (st.step()){
    			char now = st.column_string(0).charAt(0);
    			if (letter != now) {
    				letter = now;
    				sections_.add(new Section(String.valueOf(now)));
    			}
    			sections_.get(sections_.size()-1).rows.add(new Contact(
                        st.column_string(0),
                        st.column_string(1)));
    		}
    		} finally { st.close(); }
    	} finally { ab.close(); }

    CGRect outer = UIHardware.$fullScreenApplicationContentRect();
    window = new UIWindow().initWithContentRect$(outer);

    window.orderFront$(this);
    window.makeKey$(this);
    window._setHidden$(NO);

    CGRect inner = window.bounds();
    CGSize navsize = UINavigationBar.$defaultSize();
    CGRect navrect = new CGRect(0, 0, inner.size.width, navsize.height);

    UIView view = new UIView().initWithFrame$(inner);
    window.setContentView$(view);

    UINavigationBar navbar = new UINavigationBar().initWithFrame$(navrect);
    view.addSubview$(navbar);

    navbar.setBarStyle$(1);

    UINavigationItem navitem = new UINavigationItem().initWithTitle$("Contact List");
    navbar.pushNavigationItem$(navitem);

    CGRect lower = new CGRect(0, navsize.height, inner.size.width, inner.size.height - navsize.height);
    UITableView list = new UITableView().initWithFrame$(lower);
    view.addSubview$(list);

    list.setDataSource$(this);
    list.reloadData();

    /* // XXX: this doesn't work the same on 2.0 and I don't want to think about porting it right now
    AVController controller = new AVController().init();
    CharSequence wavfile = (CharSequence) ((NSBundle) NSBundle.mainBundle()).pathForResource$ofType$("start", "wav");
    AVItem wavitem = new AVItem().initWithPath$error$(wavfile, null);
    wavitem.setVolume$(100);
    controller.setCurrentItem$(wavitem);
    controller.setCurrentTime$(0);
    controller.play$(null);*/
}

public static void main(String[] args) throws Exception {
	Main.main(new String[]{HelloJava.class.getName()});
}

}
